package com.et.machine.util

import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.BuildConfig
import com.et.util.SocketUtil

class TcpBean(code: String, string: String) : TaskBase(code, string) {

    private var delayTime = 2000L
    override fun taskExc() {
        ELog.d(TAGS.machine, "TCP<<[$string]")
        SocketUtil.write(BuildConfig.tcp_server, 7007, string).subscribe({
            ELog.d(TAGS.machine, "TCP>>[$it]")
            if (it == "0") {
                TaskManager.instance?.retryTask(this.apply {
                    priority++
                })
                ELog.d(TAGS.machine, "return error -> retry TCP>>[$it] send msg:[$string]")
            }
        }, { e ->
            e.message?.let { ELog.d(TAGS.machine, "TCP ERROR<<$it") }
            TaskManager.instance?.retryTask(this)
            ELog.d(TAGS.machine, " send error -> retry TCP<<[$string]")
        })
        excDelayTask()
    }

    private fun excDelayTask() {
        try {
            Thread.sleep(delayTime * (priority + 1))
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

}